package com.cmict.data.infra.service.rdbcolumn;

import com.cmict.data.system.api.user.AdminUserApi;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.cmict.data.infra.controller.admin.rdbcolumn.vo.*;
import com.cmict.data.infra.dal.dataobject.rdbcolumn.RdbColumnDO;
import com.cmict.data.framework.common.pojo.PageResult;

import com.cmict.data.infra.convert.rdbcolumn.RdbColumnConvert;
import com.cmict.data.infra.dal.mysql.rdbcolumn.RdbColumnMapper;

import static com.cmict.data.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cmict.data.infra.enums.ErrorCodeConstants.*;

/**
 * 常用字段 Service 实现类
 *
 * @author awesome1114
 */
@Service
@Validated
public class RdbColumnServiceImpl implements RdbColumnService {

    @Resource
    private RdbColumnMapper rdbColumnMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createRdbColumn(RdbColumnCreateReqVO createReqVO) {
        // 插入
        RdbColumnDO rdbColumn = RdbColumnConvert.INSTANCE.convert(createReqVO);
        rdbColumnMapper.insert(rdbColumn);
        // 返回
        return rdbColumn.getId();
    }

    @Override
    public void updateRdbColumn(RdbColumnUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRdbColumnExists(updateReqVO.getId());
        // 更新
        RdbColumnDO updateObj = RdbColumnConvert.INSTANCE.convert(updateReqVO);
        rdbColumnMapper.updateById(updateObj);
    }

    @Override
    public void deleteRdbColumn(Long id) {
        // 校验存在
        this.validateRdbColumnExists(id);
        // 删除
        rdbColumnMapper.deleteById(id);
    }

    private void validateRdbColumnExists(Long id) {
        if (rdbColumnMapper.selectById(id) == null) {
            throw exception(RDB_COLUMN_NOT_EXISTS);
        }
    }

    @Override
    public RdbColumnDO getRdbColumn(Long id) {
        return rdbColumnMapper.selectById(id);
    }

    @Override
    public List<RdbColumnDO> getRdbColumnList(String keyword) {
        List<RdbColumnDO> list = rdbColumnMapper.getRdbColumnList(keyword);
        if(list !=null && list.size() > 0){
            for (RdbColumnDO  columnRespVO : list) {
                columnRespVO.setCreator(adminUserApi.getUser(Long.valueOf(columnRespVO.getCreator())).getCheckedData().getNickname());

            }
        }
        return list;
    }

    @Override
    public PageResult<RdbColumnRespVO> getRdbColumnPage(RdbColumnPageReqVO pageReqVO) {
        PageResult<RdbColumnDO> pageResult = rdbColumnMapper.selectPage(pageReqVO);
        PageResult<RdbColumnRespVO> respVOPageResult = RdbColumnConvert.INSTANCE.convertPage(pageResult);
        List<RdbColumnRespVO> list = respVOPageResult.getList();
        if(list !=null && list.size() > 0){
            for (RdbColumnRespVO  columnRespVO : list) {
                columnRespVO.setCreator(adminUserApi.getUser(Long.valueOf(columnRespVO.getCreator())).getCheckedData().getNickname());

            }
        }
        return respVOPageResult;
    }

    @Override
    public List<RdbColumnDO> getRdbColumnList(RdbColumnExportReqVO exportReqVO) {
        return rdbColumnMapper.selectList(exportReqVO);
    }

    @Override
    public void batchDeleteRdbColumn(Collection<Long> ids) {
        rdbColumnMapper.deleteBatchIds(ids);
    }

}
